package cn.com.smart.module.system.extDatasource.controller;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.smart.framework.aspectj.lang.annotation.Log;
import cn.com.smart.framework.aspectj.lang.enums.BusinessType;
import cn.com.smart.module.system.extDatasource.domain.ExtDatasource;
import cn.com.smart.module.system.extDatasource.service.IExtDatasourceService;
import cn.com.smart.framework.web.controller.BaseController;
import cn.com.smart.framework.web.page.TableDataInfo;
import cn.com.smart.framework.web.domain.AjaxResult;
import cn.com.smart.common.utils.poi.ExcelUtil;

/**
 * 外部扩展数据源信息操作处理
 * 
 * @author xman
 * @date 2019-07-02
 */
@Controller
@RequestMapping("/system/extDatasource")
public class ExtDatasourceController extends BaseController
{
    private String prefix = "system/extDatasource";
	
	@Autowired
	private IExtDatasourceService extDatasourceService;
	
	@RequiresPermissions("system:extDatasource:view")
	@GetMapping()
	public String extDatasource()
	{
	    return prefix + "/extDatasource";
	}
	
	/**
	 * 查询外部扩展数据源列表
	 */
	@RequiresPermissions("system:extDatasource:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ExtDatasource extDatasource)
	{
		startPage();
        List<ExtDatasource> list = extDatasourceService.selectExtDatasourceList(extDatasource);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出外部扩展数据源列表
	 */
	@RequiresPermissions("system:extDatasource:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExtDatasource extDatasource)
    {
    	List<ExtDatasource> list = extDatasourceService.selectExtDatasourceList(extDatasource);
        ExcelUtil<ExtDatasource> util = new ExcelUtil<ExtDatasource>(ExtDatasource.class);
        return util.exportExcel(list, "extDatasource");
    }
	
	/**
	 * 新增外部扩展数据源
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存外部扩展数据源
	 */
	@RequiresPermissions("system:extDatasource:add")
	@Log(title = "外部扩展数据源", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ExtDatasource extDatasource)
	{		
		if(!isConnction(extDatasource)) {
			return error("连接不正确！");
		}
		return toAjax(extDatasourceService.insertExtDatasource(extDatasource));
	}

	/**
	 * 修改外部扩展数据源
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		ExtDatasource extDatasource = extDatasourceService.selectExtDatasourceById(id);
		mmap.put("extDatasource", extDatasource);
	    return prefix + "/edit";
	}
	
	/**
	 * 分配扩展数据源
	 */
	@GetMapping("/assign/{id}")
	public String editAssignUrl(@PathVariable("id") Integer id, ModelMap mmap)
	{
		ExtDatasource extDatasource = extDatasourceService.selectExtDatasourceById(id);
		mmap.put("extDatasource", extDatasource);
	    return prefix + "/assign";
	}
	
	/**
	 * 修改保存外部扩展数据源
	 */
	@RequiresPermissions("system:extDatasource:edit")
	@Log(title = "外部扩展数据源", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ExtDatasource extDatasource)
	{		
		return toAjax(extDatasourceService.updateExtDatasource(extDatasource));
	}
	
	/**
	 * 删除外部扩展数据源
	 */
	@RequiresPermissions("system:extDatasource:remove")
	@Log(title = "外部扩展数据源", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(extDatasourceService.deleteExtDatasourceByIds(ids));
	}
	
	
	@Log(title = "测试数据源连接", businessType = BusinessType.INSERT)
	@PostMapping( "/testConn")
	@ResponseBody
	public AjaxResult testConnection(ExtDatasource extDatasource) {
		return toAjax(isConnction(extDatasource));
	}
	
	private boolean isConnction(ExtDatasource ds) {
		String username = ds.getUsername();
		String password = ds.getPassword();
		String driver =  ds.getDriver();
		String url = ds.getUrl();
		try {
			Class.forName(driver);
			DriverManager.getConnection(url, username, password);
			return true;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return false;
	}
	
}
